package com.todo.user.infrastructure.repository.impl;

import com.alibaba.fastjson.JSONObject;
import com.todo.common.common.entity.response.page.PageResult;
import com.todo.common.common.enums.BizErrorCodeEnum;
import com.todo.common.common.exception.BizException;
import com.todo.common.utils.MathUtils;
import com.todo.user.infrastructure.entity.condition.UserRelationCondition;
import com.todo.user.infrastructure.entity.po.UserRelationPO;
import com.todo.user.infrastructure.mapper.UserRelationMapper;
import com.todo.user.infrastructure.repository.UserRelationRepository;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;

/**
 * (UserRelation)表服务实现类
 *
 * @author zhangwenxiang
 * @since 2024-03-04 21:10:04
 */
@Slf4j
@Repository("userRelationRepository")
public class UserRelationRepositoryImpl implements UserRelationRepository {
    @Resource
    private UserRelationMapper userRelationMapper;


    @Override
    public Boolean insertUserRelationPO(UserRelationPO userRelationPO) {
        if (userRelationPO == null) {
            throw new BizException(BizErrorCodeEnum.PARAM_ERROR);
        }
        if (!baseFieldCheck(userRelationPO)) {
            return false;
        }
        return userRelationMapper.insertUserRelationPO(userRelationPO) > 0;
    }

    @Override
    public Boolean updateUserRelationPOById(UserRelationPO userRelationPO) {
        if (userRelationPO == null || userRelationPO.getId() == null) {
            throw new BizException(BizErrorCodeEnum.PARAM_ERROR);
        }
        if (!baseFieldCheck(userRelationPO)) {
            return false;
        }
        return userRelationMapper.updateUserRelationPOById(userRelationPO) > 0;
    }


    @Override
    public Boolean removeUserRelationPOById(Long id) {
        if (id == null || id < 0) {
            throw new BizException(BizErrorCodeEnum.PARAM_ERROR);
        }
        return userRelationMapper.removeUserRelationPOById(id) > 0;
    }

    @Override
    public UserRelationPO queryUserRelationPOById(Long id) {
        if (id == null || id < 0) {
            return null;
        }
        return userRelationMapper.queryUserRelationPOById(id);
    }

    @Override
    public PageResult<UserRelationPO> queryUserRelationPOByCondition(UserRelationCondition userRelationCondition) {
        PageResult<UserRelationPO> pageResult = new PageResult<>();
        if (userRelationCondition == null) {
            throw new BizException(BizErrorCodeEnum.DATA_NOT_EXIST);
        }
        pageResult.setPageNum(userRelationCondition.getPageIndex());
        pageResult.setPageSize(userRelationCondition.getPageSize());
        long count = queryCountByCondition(userRelationCondition);
        if (count <= 0) {
            return pageResult;
        }
        pageResult.setPageCount((int) MathUtils.divide(count, userRelationCondition.getPageSize()));
        pageResult.setTotal(count);
        List<UserRelationPO> userAuthPOList = userRelationMapper.queryUserRelationByCondition(userRelationCondition);
        if (CollectionUtils.isEmpty(userAuthPOList)) {
            pageResult.setHasNext(false);
            return pageResult;
        }
        pageResult.setResults(userAuthPOList);
        pageResult.setCurrentIndex(userAuthPOList.get(0).getId());
        pageResult.setHasNext(userAuthPOList.size() >= userRelationCondition.getPageSize());
        return pageResult;
    }

    @Override
    public Long queryCountByCondition(UserRelationCondition userRelationCondition) {
        if (userRelationCondition == null || userRelationCondition.getPageSize() <= 0) {
            throw new BizException(BizErrorCodeEnum.DATA_NOT_EXIST);
        }
        return userRelationMapper.queryCountByCondition(userRelationCondition);
    }

    @Override
    public UserRelationPO queryUserRelationByUserId(Long userId, Long relationUserId) {
        if (userId == null || userId < 0 || relationUserId == null || relationUserId < 0){
            return null;
        }
        return userRelationMapper.queryUserRelationByUserId(userId, relationUserId);
    }

    private boolean baseFieldCheck(UserRelationPO userRelationPO) {
        if (userRelationPO == null) {
            return false;
        }
        if (userRelationPO.getUserId() == null || userRelationPO.getRelationUserId() == null || userRelationPO.getRelationType() == null) {
            log.warn("UserRelationRepository#baseFieldCheck 核心字段校验失败，操作失败 userRelationPO：【{}】", JSONObject.toJSONString(userRelationPO));
            return false;
        }
        return true;
    }
}
